package com.wawei.seeds.me;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBarActivity;

import com.wawei.seeds.R;
import com.wawei.seeds.receiver.PushGetuiReceiver;

import org.json.JSONObject;

import butterknife.ButterKnife;

/**
 *
 * Created by t27891 on 15/6/21.
 */
public class MeMainActivity extends ActionBarActivity implements MeFragment{
    public static final int HOME = 0;
    public static final int APPOINTMENT = 1;
    public static final int SERVICE = 2;
    public static final int FAVORITE = 3;
    public static final int SETTING = 4;
    public static final int SELF_PORTRAIT = 5;
    public static final int EXPERT = 6;
    public static final int APPOINTMENT_ME = 7;

    private int currentFragmentTag = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_walnut_me_main);
        ButterKnife.inject(this);

        init();
    }

    @Override
    public void onSelectFragment(int tag) {
        if(currentFragmentTag == tag)
            return;

        FragmentManager fragmentManager = getSupportFragmentManager();
        Fragment currentFragment = fragmentManager.findFragmentByTag(String.valueOf(currentFragmentTag));
        if(null != currentFragment){
            fragmentManager.beginTransaction().hide(currentFragment).commit();
            currentFragmentTag = -1;
        }
        currentFragment = fragmentManager.findFragmentByTag(String.valueOf(tag));
        if(null != currentFragment){
            fragmentManager.beginTransaction().show(currentFragment).commit();
            currentFragmentTag = tag;
        }
        else {
            switch (tag) {
                case HOME:
                    Bundle bundle = new Bundle();
                    bundle.putString("me", "id");
                    MeMainFragment meMainFragment = new MeMainFragment();
                    meMainFragment.setArguments(bundle);
                    getSupportFragmentManager().beginTransaction().add(R.id.meMainContainer, meMainFragment, String.valueOf(tag)).commit();
                    currentFragmentTag = tag;
                    break;
                case APPOINTMENT:
                    MeAppointmentFragment meAppointmentFragment = new MeAppointmentFragment();
                    getSupportFragmentManager().beginTransaction().add(R.id.meMainContainer, meAppointmentFragment, String.valueOf(tag)).commit();
                    currentFragmentTag = tag;
                    break;
                case SERVICE:
                    MeServiceFragment serviceFragment = new MeServiceFragment();
                    getSupportFragmentManager().beginTransaction().add(R.id.meMainContainer, serviceFragment, String.valueOf(tag)).commit();
                    currentFragmentTag = tag;
                    break;
                case FAVORITE:
                    MeFavoriteFragment meFavoriteFragment = new MeFavoriteFragment();
                    getSupportFragmentManager().beginTransaction().add(R.id.meMainContainer, meFavoriteFragment, String.valueOf(tag)).commit();

                    currentFragmentTag = tag;
                    break;
                case SETTING:
                    MeSettingsFragment settingsFragment = new MeSettingsFragment();
                    getSupportFragmentManager().beginTransaction().add(R.id.meMainContainer, settingsFragment, String.valueOf(tag)).commit();
                    currentFragmentTag = tag;
                    break;
                case SELF_PORTRAIT:
                    MeSelfPortraitFragment selfPortraitFragment = new MeSelfPortraitFragment();
                    getSupportFragmentManager().beginTransaction().add(R.id.meMainContainer, selfPortraitFragment, String.valueOf(tag)).commit();
                    currentFragmentTag = tag;
                    break;
                case EXPERT:
                    MeExpertFragment expertFragment = new MeExpertFragment();
                    getSupportFragmentManager().beginTransaction().add(R.id.meMainContainer, expertFragment, String.valueOf(tag)).commit();
                    currentFragmentTag = tag;
                    break;
                case APPOINTMENT_ME:
                    AppointmentMeFragment appointmentMeFragment = new AppointmentMeFragment();
                    getSupportFragmentManager().beginTransaction().add(R.id.meMainContainer, appointmentMeFragment, String.valueOf(tag)).commit();
                    currentFragmentTag = tag;
                    break;
                default:
                    currentFragmentTag = -1;
                    break;
            }
        }
    }

    private void init(){
        String data = this.getIntent().getStringExtra("data");

        if (data != null && data.length() > 0) {
            try {
                JSONObject object = new JSONObject(data);

                String action = object.optString("action");

                if (PushGetuiReceiver.APPOINTMENT_ME.equals(action)) {
                    onSelectFragment(APPOINTMENT_ME);

                    return;
                } else if (PushGetuiReceiver.ME_APPOINTMENT_ACCEPT.equals(action) || PushGetuiReceiver.ME_APPOINTMENT_REJECT.equals(action)) {
                    onSelectFragment(APPOINTMENT);
                }
            } catch (Exception e) {

            }
        }

        onSelectFragment(HOME);
    }
}
